package com.festibiere.pointage;

import android.app.Activity;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class festibierePointage extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
	
	TextView txtView11,txtView21,txtView31,txtView41,txtView51,txtView61,txtView71,txtView81,txtView91,txtView101;
	TextView txtView12,txtView22,txtView32,txtView42,txtView52,txtView62,txtView72,txtView82,txtView92,txtView102;
	EditText  editText1, editText2,editText3, editText4,editText5, editText6,editText7, editText8,editText9, editText10;
	
	double meilleurTemps;
	
	Button BtnScore, BtnReset;
	
	TextView [][] txtViewArray = new TextView[10][2];
	EditText[] editTextArray = new EditText[10];
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        txtViewArray[0][0] = (TextView) this.findViewById(R.id.TextView11);
        txtViewArray[0][1]= (TextView) this.findViewById(R.id.TextView12);
        txtViewArray[1][0] = (TextView) this.findViewById(R.id.TextView21);
        txtViewArray[1][1]= (TextView) this.findViewById(R.id.TextView22);
        txtViewArray[2][0] = (TextView) this.findViewById(R.id.TextView31);
        txtViewArray[2][1]= (TextView) this.findViewById(R.id.TextView32);
        txtViewArray[3][0] = (TextView) this.findViewById(R.id.TextView41);
        txtViewArray[3][1]= (TextView) this.findViewById(R.id.TextView42);
        txtViewArray[4][0] = (TextView) this.findViewById(R.id.TextView51);
        txtViewArray[4][1]= (TextView) this.findViewById(R.id.TextView52);
        txtViewArray[5][0] = (TextView) this.findViewById(R.id.TextView61);
        txtViewArray[5][1]= (TextView) this.findViewById(R.id.TextView62);
        txtViewArray[6][0] = (TextView) this.findViewById(R.id.TextView71);
        txtViewArray[6][1]= (TextView) this.findViewById(R.id.TextView72);
        txtViewArray[7][0] = (TextView) this.findViewById(R.id.TextView81);
        txtViewArray[7][1]= (TextView) this.findViewById(R.id.TextView82);
        txtViewArray[8][0] = (TextView) this.findViewById(R.id.TextView91);
        txtViewArray[8][1]= (TextView) this.findViewById(R.id.TextView92);
        txtViewArray[9][0] = (TextView) this.findViewById(R.id.TextView101);
        txtViewArray[9][1]= (TextView) this.findViewById(R.id.TextView102);
        
        editTextArray[0] = (EditText) this.findViewById(R.id.EditText01);
        editTextArray[1] = (EditText) this.findViewById(R.id.EditText02);
        editTextArray[2] = (EditText) this.findViewById(R.id.EditText03);
        editTextArray[3] = (EditText) this.findViewById(R.id.EditText04);
        editTextArray[4] = (EditText) this.findViewById(R.id.EditText05);
        editTextArray[5] = (EditText) this.findViewById(R.id.EditText06);
        editTextArray[6] = (EditText) this.findViewById(R.id.EditText07);
        editTextArray[7] = (EditText) this.findViewById(R.id.EditText08);
        editTextArray[8] = (EditText) this.findViewById(R.id.EditText09);
        editTextArray[9] = (EditText) this.findViewById(R.id.EditText10);
        
        for(int i =0 ; i<10;i++)
        {
        	editTextArray[i].setInputType(InputType.TYPE_CLASS_PHONE);
        }
        
        
        txtViewArray[0][0].setText("Guillemette Family");
        txtViewArray[1][0].setText("Danger!");
        txtViewArray[2][0].setText("aWEsome Pirates");
        txtViewArray[3][0].setText("Les Chaud'hiers");
        txtViewArray[4][0].setText("Epic Beer Time");
        txtViewArray[5][0].setText("Girls Angels");
        txtViewArray[6][0].setText("CowBeers");
        txtViewArray[7][0].setText("Lazariens Power");
        txtViewArray[8][0].setText("EPIC");
        txtViewArray[9][0].setText("Soeur à Joey");
      //setup des équipes
     
         
      BtnScore = (Button) this.findViewById(R.id.BtnScore);
      BtnReset =(Button) this.findViewById(R.id.btnReset);
       BtnScore.setOnClickListener(this);
       BtnReset.setOnClickListener(this);
    }

	

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
		switch(arg0.getId())
		{
			case R.id.BtnScore:
			{
				meilleurTemps = calculMeilleurTemps();
				
				txtViewArray[3][1].setText(""+meilleurTemps);
				actualiserScore();
				break;
			}
			case R.id.btnReset:
			{
				for(int i = 0;i<10;i++)
				{
					txtViewArray[i][1].setText("      --      ");
					editTextArray[i].setText("");
				}
				break;
			}
		}
		
		
		
		
		
		//temps = Double.parseDouble(boxTemps.toString());
		
		
	}
	
	public double calculMeilleurTemps()
	{
		int meilleur = Integer.parseInt(editTextArray[0].getText().toString());
		int current;
		
		for(int i = 0;i<10;i++)
		{
			
			
				
			if(editTextArray[i].getText().toString().length()==0 || editTextArray[i].getText().equals(""))
			{
				current = 9999;
			}
			else
			{
				current = Integer.parseInt(editTextArray[i].getText().toString());
			}
		
		
				if(current < meilleur)
				{
					meilleur = current;//(int)(Integer.parseInt(editTextArray[i].getText().toString()));
					
				}
			
		}

		return (double) meilleur;
		
	}
	
	public void actualiserScore()
	{
		
		double rapport,current;
		for(int i = 0;i<10;i++)
		{
			
			if(editTextArray[i].getText().toString().length()==0  || editTextArray[i].getText().equals(""))
			{
				current = 9999.9;
			}
			else 
			{
				current = Double.valueOf(editTextArray[i].getText().toString()).doubleValue();
			}
			rapport = ((meilleurTemps)/current)*4;
			
			txtViewArray[i][1].setText(String.format("%.4g%n", rapport));
			
		}
	}
	
	
}